package com.example.lvsuo.aop;


import com.auth0.jwt.interfaces.Claim;
import com.example.lvsuo.modal.Result;
import com.example.lvsuo.service.ISystemUserService;
import com.example.lvsuo.service.cxg.FlowDataService;
import com.example.lvsuo.service.cxg.GetNewIdServlet;
import com.example.lvsuo.service.impl.SystemAuthorityServiceImpl;
import com.example.lvsuo.service.impl.SystemUserServiceImpl;
import com.example.lvsuo.utils.JwtUtils;
import jdk.nashorn.internal.parser.Token;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;


@Component
@Aspect
public class FlowAop {
    @Autowired
    GetNewIdServlet getNewIdServlet;
    @Autowired
    FlowDataService flowDataService;
    @Autowired
    SystemAuthorityServiceImpl authorityService;
    @Autowired
    SystemUserServiceImpl userService;

    @Around("@annotation(f)")
    public Result flowDome(ProceedingJoinPoint pjp, Flow f) throws Throwable {

        String value = f.KUAI_ENUM().getValue();
        String authorId = value.split(",")[0];// 通过注解参数 获取系统权限id

//        System.out.println("模块Id为:"+authorId);

        String fid = authorityService.getSystemAuthorityByid(authorId); // 流程id

//        System.out.println("流程id为:"+fid);

        Result result = (Result) pjp.proceed();
        int id = getNewIdServlet.getNewId();//需要审批的 id

//        Object[] args = pjp.getArgs();
//        System.out.println(args[0]);

//
        String tName = f.tName(); // 表名

        //获取request对象
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        Object[] args = pjp.getArgs();//拿到方法的参数
        String requestURI = request.getRequestURI();//拿到请求的url
        //获取对象头中的这个信息
//        String fid = request.getHeader("fid");//获取流程ID
//        String applicant = request.getHeader("applicant");//获取申请人

        String token = request.getHeader("token");// 获取token
        Map<String, Claim> check = JwtUtils.check(token);// 获取用户信息

//        System.out.println("token解析==>"+check);

//        通过用户名查询 当前登陆者ID
        String userName = check.get("user").toString();// 获取用户名

//        System.out.println("token获得的userName==>"+userName);

         String applicant = userService.getUserByName(userName);// 通过用户名查询ID

//        System.out.println("token获得的申请人id==>"+applicant);


//        System.out.println("check为===>"+check);


//        System.out.println("fid==>"+fid);//流程id
//        System.out.println("applicant==>"+applicant);//申请人id
//        System.out.println("id==>"+id);//审批的数据id
//        System.out.println("tName==>"+tName);//表名

        Map map = new HashMap();
        map.put("fid",fid);
        map.put("applicant",applicant);
        map.put("id",id);
        map.put("tName",tName);

        flowDataService.addFlowData(map);






        return result;

    }



}
